JMU JMU - Department of Computer Science
Help Tools
C Programming Style Guide


1 Local Style Guide

You must follow certain guidelines when writing C code.
  • Type names must start with an uppercase letter.

    In addition, each "word" within a class name should start with an uppercase letter. For example, TwoDimensionalPoint is an appropriate type names.

  • Variable and method/function names that contain multiple characters must not start with an uppercase letter.

    Further, each "word" within a variable name should start with an uppercase letter. For example, importantMessage and campusMonitor are both appropriate variable names.

  • Variable names that consist of a single character may be uppercase.

    In general, even single-character variable names should be lowercase. However, in some situations, mathematical notation uses uppercase letters. In such situations, uppercase variable names may be used. For example, matrices are often written using uppercase letters. So, an expression like (b = A*x) would be appropriate.

  • Variable and method/function names must be descriptive.

    Variable names like aaa are not appropriate. Index variables and counters can, however, have names like i and j.

  • User-defined constants must be entirely in uppercase.
  • All variables must be declared at the top of the relevant function (with one exception).

    The only exception to this rule is index variables in loops, which may be declared locally.

  • All variables must be declared alphabetically by their type.

    For example, variables of type double should be declared before variables of type int which should, in turn, be declared before variables of type Node.

  • Use indentation and use it consistently.

    No particular indentation scheme is required but you must indent your code in a meaningful and consistent way.

  • Comments must be in Doxygen format.

    The Javadoc format is preferred, but not required.

  • File comments must be in the .h file and function comments must be in the .c file.

    Since Doxygen combines comments from both types of files, this approach works for both people that do and don't have access to the source.

  • File comments must a description and the author's name.

    It must also attest to your compliance with the JMU Honor Code.

  • Function comments must include a list of parameters and a description of what is returned (if anything).

    It must also list any side effects.

2 Other Style Guides

Different people have different opinions about what is "stylish". If you are interested in such things (and you should be) you might want to take a look at:

There are also several books devoted to C style, including:

Ranade, J. and A. Nash (1992) The Elements of C Programming Style, McGraw Hill , Boston.
(Order from amazon , order from Barnes and Noble , compare at bigwords , compare at CampusBooks4Less , order from Chegg , or search eFollett )

3 Style Humor

As mentioned above, different people have different opinions about what is "stylish". This leads to many fights (and some jokes).

../lectures/comics/Hackles-Style.png
(Courtesy of Hackles)

http://imgs.xkcd.com/comics/code_quality.png
(Courtesy of xkcd)

http://imgs.xkcd.com/comics/code_quality_2.png
(Courtesy of xkcd)

Copyright 2019